import Decimal from "decimal.js";

/**
 * @Description:
 * @author 云离
 * @date: 2020-02-10
 */
export function accSub(arg1, arg2) {
  var r1, r2, m, n;
  try {
    r1 = arg1.toString().split(".")[1].length;
  } catch (e) {
    r1 = 0;
  }
  try {
    r2 = arg2.toString().split(".")[1].length;
  } catch (e) {
    r2 = 0;
  }
  m = Math.pow(10, Math.max(r1, r2));
  // 动态控制精度长度
  n = r1 >= r2 ? r1 : r2;
  return ((arg1 * m - arg2 * m) / m).toFixed(n);
}

export function addMoney(amount1, amount2) {
  const decimal1 = new Decimal(amount1);
  const decimal2 = new Decimal(amount2);

  const total = decimal1.plus(decimal2);
  return total.toNumber();
}

export function subtractMoney(amount1, amount2) {
  const decimal1 = new Decimal(amount1);
  const decimal2 = new Decimal(amount2);

  const difference = decimal1.minus(decimal2);
  return difference.toNumber();
}

export function multiplyMoney(amount, multiplier) {
  const decimalAmount = new Decimal(amount);
  const decimalMultiplier = new Decimal(multiplier);

  const product = decimalAmount.mul(decimalMultiplier);
  return product.toNumber();
}

export function divideMoney(amount, divisor) {
  const decimalAmount = new Decimal(amount);
  const decimalDivisor = new Decimal(divisor);

  const quotient = decimalAmount.div(decimalDivisor);
  return quotient.toNumber();
}
